
;
(function () {

    $(function () {
        //入口函数
        $.idcode.setCode(); //加载生成验证码方法

        $("form").validate({
            rules: {
                uname: {
                    required: true, //必填
                    rangelength: [3, 50],
                    // checkName: true
                },
                upwd: {
                    required: true, //必填
                    rangelength: [6, 20],
                }

            },
            messages: {
                uname: {
                    required: '用户名必填',
                    rangelength: "用户名必须是{0}-{1}",
                },
                upwd: {
                    required: '密码必填',
                    rangelength: "密码必须是{0}-{1}"
                }
            },

            submitHandler: function () {

                var IsBy = $.idcode.validateCode();

                if (!IsBy) {

                    layer.msg('验证码错误!');
                    return false;
                } else {
                    let formdata = new FormData();
                    formdata.append("u_name", $('[name="uname"]').val());
                    formdata.append("u_pwd", $('[name="upwd"]').val());
                    console.log(formdata);
                    // 与数据库匹配,并返回res
                    $.ajax({
                        url: "./api/login",
                        data: formdata,
                        type: "post",
                        processData: false,
                        contentType: false
                    }).done(function (res) {
                        //注册成功,是否理解去登录 状态为1
                        if (res.status == 1) {
                            localStorage.setItem("users", JSON.stringify(res.data));
                            layer.confirm(`${res.msg},是否立即去首页?`, {
                                skin: 'layui-layer-molv',
                                type: 1,
                                btn: ["yes", "noyes"]
                            }, (index) => {
                                layer.close(index);
                                location = "./index.html";
                            }, () => { });
                        } else {
                            layer.msg(res.msg);
                        }
                    })
                    return false;
                }
            }
        });

         $("#register").hover(function () {
             $(this).addClass("click");
         }, function () {
             $(this).removeClass("click");
         });
         $("#login").hover(function () {
             $(this).addClass("click");
         }, function () {
             $(this).removeClass("click");
         });
         $("#register").click(function () {
     
             document.location.href = "./register.html";
             return false;
         });
    })
})();